// pages/addsite_address/addsite_address.js
const app = getApp();
import { u_Reg } from '../../config/tool.js';
import areaList from '../../config/area.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 0, // 0正常 1修改
    region: ['江西省','南昌市','青云谱区'],
    address: ['江西省','南昌市','青云谱区'],
    infos: '', // 要修改的地址信息
    name: '', // 姓名
    tel: '', // 手机号
    addressName: '', // 省市区
    detail: '', // 详细地址
    isDefault: false, // 是否默认地址
    areaList: areaList,
    show: false, // 展示省市区弹窗
    schoolId: '', // 校区id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if(options && options.type == 1){
      let data = JSON.parse(options.data);
      let news = [];
      news[0] = data.province;
      news[1] = data.city;
      news[2] = data.region;
      this.setData({
        infos: data,
        type: options.type,
        region: news,
        name: data.name, // 姓名
        tel: data.phone, // 手机号
        addressName: data.province +' '+ data.city+ ' ' + data.region, // 省市区
        detail: data.detail, // 详细地址
        isDefault: data.isdefault == 1 ? true : false, // 是否默认地址
      })
      wx.setNavigationBarTitle({
        title: '修改地址',
      })
    }

    if (options.schoolId){
      this.setData({
        schoolId: options.schoolId
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  // 输入姓名
  initName(e){
    this.setData({
      name: e.detail.value
    })
  },

  // 输入手机号
  initTel(e){
    this.setData({
      tel: e.detail.value
    })
  },

  // 选择省市区
  bindRegionChange(e){
    this.setData({
      address: e.detail.value,
      addressName: e.detail.value[0] + ' ' + e.detail.value[1] + ' ' + e.detail.value[2]
    })
  },
  showList(){
    this.setData({
      show: true
    })
  },
  change(e){
    // console.log(e)
  },
  confirm(e){
    this.setData({
      address: e.detail.values,
      addressName: e.detail.values[0].name + ' ' + e.detail.values[1].name + ' ' + e.detail.values[2].name,
      show: false
    })
  },
  cancel(){
    this.setData({
      show: false
    })
  },

  // 输入详细地址
  initDetail(e){
    this.setData({
      detail: e.detail.value
    })
  },

  // 设置默认地址
  setDefault(){
    this.setData({
      isDefault: !this.data.isDefault
    })
  },

  // 提交
  submit(){
    if(!this.data.name){
      app.$tip('请输入收货人姓名!');
      return
    }
    if(!this.data.tel){
      app.$tip('请输入手机号码!');
      return
    }
    if (!u_Reg(this.data.tel,'phone')){
      app.$tip('请输入正确的手机号');
      return
    }
    if(!this.data.schoolId){
      if (!this.data.addressName){
        app.$tip('请选择地区!');
        return
      }
    }
    if(!this.data.detail){
      app.$tip('请输入详细地址!');
      return
    }

    this.reqData();
  },

  //请求
  reqData(){
    // 修改地址
    if(this.data.type == 1){
      let that = this;
      app.loading();
      wx.request({
        url: app.baseURL + 'Adress/updateAddress',
        data: {
          token: app.globalData.userId,
          address_id: that.data.infos.address_id,
          province: that.data.schoolId ? '' : that.data.address[0].name,
          city: that.data.schoolId ? '' : that.data.address[1].name,
          region: that.data.schoolId ? '' : that.data.address[2].name,
          detail: that.data.detail,
          name: that.data.name,
          phone: that.data.tel,
          isdefault: that.data.isDefault ? 1 : 0,
          school_id: that.data.schoolId
        },
        success(data) {
          let res = data.data;
          wx.hideLoading();
          if (res.code == 1) {
            app.$tip(res.msg);
            setTimeout(() => {
              wx.navigateBack()
            }, 1500)
          } else {
            app.$tip(res.msg);
          }
        }
      })
    }else{
      // 新增地址
      let that = this;
      app.loading();
      wx.request({
        url: app.baseURL + 'Adress/addAddress',
        data: {
          token: app.globalData.userId,
          phone: that.data.tel,
          name: that.data.name,
          province: that.data.schoolId ? '' : that.data.address[0].name,
          city: that.data.schoolId ? '' : that.data.address[1].name,
          region: that.data.schoolId ? '' : that.data.address[2].name,
          details: that.data.detail,
          isdefault: that.data.isDefault ? 1 : 0,
          school_id: that.data.schoolId
        },
        success(data) {
          let res = data.data;
          wx.hideLoading();
          if (res.code == 1) {
            app.$tip(res.msg);
            setTimeout(() => {
              wx.navigateBack()
            }, 1500)
          } else {
            app.$tip(res.msg);
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },


})